package org.bigdata.framework.admin.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bigdata.framework.core.model.CommunityCategory;
import org.bigdata.framework.core.model.CommunityDomain;
import org.bigdata.framework.core.model.LiveRoom;
import org.bigdata.framework.core.model.LiveStream;
import org.bigdata.framework.core.protocol.ICommunityService;
import org.bigdata.framework.core.protocol.ILiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xxl.conf.core.XxlConfClient;
@Controller
public class ZhiboController extends BaseContoroller{
	@Autowired
	private ILiveService liveService;
	
	// 前往流媒体管理页面
    @RequestMapping("zhibo-live-stream.html")
    public String toStream() {
        return "/modules/zhibo/zhibo_live_stream";
    }
    // 前往直播间管理页面
    @RequestMapping("zhibo-live-room.html")
    public String toRoom() {
    	return "/modules/zhibo/zhibo_live_room";
    }
    // 前往流媒体编辑添加页面
    @RequestMapping("zhibo_live_stream_add_update.html")
    public String toAddOrUpdate() {
    	return "/modules/zhibo/zhibo_live_stream_add_update";
    }
    // 前往直播间编辑添加页面
    @RequestMapping("zhibo_live_room_add_update.html")
    public String roomToAddOrUpdate() {
    	return "/modules/zhibo/zhibo_live_room_add_update";
    }
    // 前往选择用户名
    @RequestMapping("zhibo_live_room_user.html")
    public String roomToUser() {
    	return "/modules/zhibo/zhibo_live_room_user";
    }
    // 前往选择用户名
    @RequestMapping("zhibo_live_room_media.html")
    public String roomToMedia() {
    	return "/modules/zhibo/zhibo_live_room_media";
    }
    //获得流媒体列表
    @RequestMapping("getLiveStreamList")
    public void getLiveStreamList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer pageSize = Integer.parseInt(request.getParameter("page"));
        Integer number = Integer.parseInt(request.getParameter("number"));
        String queryParam = request.getParameter("queryParams");//检索选择类别
        String text = request.getParameter("text");//检索关键字
        //拼接-筛选条件
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        if (text != null && !"".equals(text)) {
            queryParams.put("%," + queryParam, text);
        }
        //得到结果集
        List<LiveStream> liveStreamList = liveService.getLiveStream(queryParams, pageSize, number);
        List<LinkedHashMap<String, Object>> data = new ArrayList<LinkedHashMap<String,Object>>();
        LinkedHashMap<String, Object> map = null;
        for (int i = 0; i < liveStreamList.size(); i++) {
            map = new LinkedHashMap<String, Object>();
            LiveStream liveStream = liveStreamList.get(i);
            map.put("id", liveStream.getId());
            map.put("title", liveStream.getTitle());
            map.put("url", liveStream.getUrl());
            map.put("description", liveStream.getDescription());
            data.add(map);
        }
        Integer count = liveService.getLiveStreamCount(queryParams);//得到总数
        LinkedHashMap<String, Object> r =new LinkedHashMap<String, Object>();
        r.put("data", data);
        r.put("count", count);
        r.put("page", (int)Math.ceil(Double.valueOf(count)/number));//得到分页数
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
  //流媒体--修改单个
    @RequestMapping("findLiveStreamList")
    public void findLiveStreamList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        LiveStream findLiveStream = liveService.findLiveStream(id);
        LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("title", findLiveStream.getTitle());
        map.put("url", findLiveStream.getUrl());
        map.put("description", findLiveStream.getDescription());
        map.put("id", findLiveStream.getId());
        String resultString = formatToJson(map);
        super.pringWriterToPage(resultString, "application/json", response);
    }
  //流媒体--保存
    @RequestMapping("addOrUploadLiveStream")
    public void addOrUploadLiveStream(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        String url = request.getParameter("url");
        String description = request.getParameter("description");
        LiveStream liveStream = new LiveStream();
        liveStream.setDescription(description);
        liveStream.setTitle(title);
        liveStream.setUrl(url);
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        try {
            if("0".equals(id)){
            	liveService.saveLiveStream(liveStream);
            }else{
            	liveStream.setId(Integer.parseInt(id));
                liveService.editLiveStream(liveStream);
            }
            r.put("data", "yes");
        } catch (Exception e) {
            r.put("data", "no");
        }
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
  //流媒体--删除
    @RequestMapping("deldeteLiveStream")
    public void deldeteLiveStream(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String rData="yes";
        try {
        	HashMap<String, Object> queryParams = new HashMap<String, Object>();
            queryParams.put("=,liveStreamId",id);
            List<LiveRoom> liveRoomList = liveService.getLiveRoom(queryParams,0,5);
        	if(liveRoomList.size()<1){
        		LiveStream findLiveStream = liveService.findLiveStream(id);
                liveService.deleteLiveStream(findLiveStream);
        	}else{
        		 rData = "no";
        	}
        } catch (Exception e) {
            rData = "no";
        }
        List<String> r = new ArrayList<String>();
        r.add(rData);
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //直播间--列表
    @RequestMapping("getLiveRoomList")
    public void getLiveRoomList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer pageSize = Integer.parseInt(request.getParameter("page"));
        Integer number = Integer.parseInt(request.getParameter("number"));
        String queryParam = request.getParameter("queryParams");
        String text = request.getParameter("text");
        //拼接-筛选条件
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        if (text != null && !"".equals(text)) {
            queryParams.put("%," + queryParam, text);
        }
        queryParams.put("^,createTime", "asc");
        //得到结果集
        List<LiveRoom> liveRoomList = liveService.getLiveRoom(queryParams, pageSize, number);
        List<LinkedHashMap<String, Object>> data = new ArrayList<LinkedHashMap<String,Object>>();
        LinkedHashMap<String, Object> map = null;
        for (int i = 0; i < liveRoomList.size(); i++) {
            map = new LinkedHashMap<String, Object>();
            LiveRoom liveRoom = liveRoomList.get(i);
            map.put("liveTitle", liveRoom.getLiveTitle());//
            map.put("liveNotice", liveRoom.getLiveNotice());
            String uploadPath =  XxlConfClient.get("dal.file.server", "") +"/";
            map.put("liveCover", uploadPath+liveRoom.getLiveCover());//
            map.put("id", liveRoom.getId());
            map.put("liveStreamId", liveRoom.getLiveStreamId());
            map.put("liveStreamTitle", liveService.findLiveStream(liveRoom.getLiveStreamId()).getTitle());
            
            map.put("status", liveRoom.getStatus());//
            map.put("liveStatus", liveRoom.getLiveStatus());//
            map.put("anchorUsername", liveRoom.getAnchorUsername());//
            map.put("anchorNumber", liveRoom.getAnchorNumber());//
            map.put("createTime", liveRoom.getCreateTime());
            data.add(map);
        }
        Integer count = liveService.getLiveRoomCount(queryParams);//得到总数
        LinkedHashMap<String, Object> r =new LinkedHashMap<String, Object>();
        r.put("data", data);
        r.put("count", count);
        r.put("page", (int)Math.ceil(Double.valueOf(count)/number));//得到分页数
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
  //直播间--修改单个
    @RequestMapping("findLiveRoomList")
    public void findLiveRoomList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        LiveRoom liveRoom = liveService.findLiveRoom(id);
        LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("liveTitle", liveRoom.getLiveTitle());//
        map.put("liveNotice", liveRoom.getLiveNotice());
        String uploadPath =  XxlConfClient.get("dal.file.server", "") +"/";
        map.put("liveCover", uploadPath+liveRoom.getLiveCover());//
        map.put("img", liveRoom.getLiveCover());//
        
        map.put("livePassword", liveRoom.getLivePassword());//
        map.put("liveStreamId", liveRoom.getLiveStreamId());//
        map.put("liveStreamTitle", liveService.findLiveStream(liveRoom.getLiveStreamId()).getTitle());
        
        map.put("anchorUsername", liveRoom.getAnchorUsername());//
        map.put("anchorNumber", liveRoom.getAnchorNumber());//
        map.put("status", liveRoom.getStatus());//
        map.put("liveHtml", liveRoom.getLiveHtml());//
        map.put("id", liveRoom.getId());
        map.put("liveStatus", liveRoom.getLiveStatus());//
        map.put("createTime", liveRoom.getCreateTime());
        String resultString = formatToJson(map);
        super.pringWriterToPage(resultString, "application/json", response);
    }
  //直播间管理--保存
    @RequestMapping("addOrUploadLiveRoom")
    public void addOrUploadLiveRoom(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
		String liveTitle =request.getParameter("liveTitle");
		String liveNotice =request.getParameter("liveNotice");
		String liveCover = request.getParameter("liveCover");
		String livePassword = request.getParameter("livePassword");
		String liveStreamId = request.getParameter("liveStreamId");
		String anchorUsername = request.getParameter("anchorUsername");
		String anchorNumber = request.getParameter("anchorNumber");
		String status = request.getParameter("status");
		String liveHtml = request.getParameter("liveHtml");
		String liveStatus = request.getParameter("liveStatus");
		String createTime = request.getParameter("createTime");
		if(createTime.length()<1){
			Date day=new Date();    
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			createTime = df.format(day);
		}
		LiveRoom liveRoom = new LiveRoom();
		liveRoom.setLiveTitle(liveTitle);
		liveRoom.setLiveNotice(liveNotice);
		liveRoom.setLiveCover(liveCover);
		liveRoom.setLivePassword(livePassword);
		liveRoom.setLiveStreamId(Integer.parseInt(liveStreamId));
		liveRoom.setAnchorUsername(anchorUsername);
		liveRoom.setAnchorNumber(anchorNumber);
		liveRoom.setStatus(Integer.parseInt(status));
		liveRoom.setLiveHtml(liveHtml);
		liveRoom.setLiveStatus(Integer.parseInt(liveStatus));
		liveRoom.setCreateTime(createTime);
        
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        try {
            if("0".equals(id)){
            	liveService.saveLiveRoom(liveRoom);
            }else{
            	liveRoom.setId(Integer.parseInt(id));
                liveService.editLiveRoom(liveRoom);
            }
            r.put("data", "yes");
        } catch (Exception e) {
            r.put("data", "no");
        }
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
  //直播间--删除
    @RequestMapping("deldeteLiveRoom")
    public void deldeteLiveRoom(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String rData="yes";
        try {
            LiveRoom findLiveRoom = liveService.findLiveRoom(id);
            liveService.deleteLiveRoom(findLiveRoom);
        } catch (Exception e) {
            rData = "no";
        }
        List<String> r = new ArrayList<String>();
        r.add(rData);
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
  //直播间--审批
    @RequestMapping("subjectLiveRoom")
    public void subjectLiveRoom(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Integer parameter = Integer.parseInt(request.getParameter("parameter"));
        String rData="yes";
        try {
            LiveRoom findLiveRoom = liveService.findLiveRoom(id);
            findLiveRoom.setStatus(parameter);
            liveService.editLiveRoom(findLiveRoom);
        } catch (Exception e) {
            rData = "no";
        }
        List<String> r = new ArrayList<String>();
        r.add(rData);
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //直播间--禁播处理
    @RequestMapping("bannedLiveRoom")
    public void bannedLiveRoom(HttpServletRequest request,HttpServletResponse response) throws Exception {
    	Integer id = Integer.parseInt(request.getParameter("id"));
    	Integer parameter = Integer.parseInt(request.getParameter("parameter"));
    	String rData="yes";
    	try {
    		LiveRoom findLiveRoom = liveService.findLiveRoom(id);
    		findLiveRoom.setLiveStatus(parameter);
    		liveService.editLiveRoom(findLiveRoom);
    	} catch (Exception e) {
    		rData = "no";
    	}
    	List<String> r = new ArrayList<String>();
    	r.add(rData);
    	String resultString = formatToJson(r);
    	super.pringWriterToPage(resultString, "application/json", response);
    }
}
